package cn.dengta.webapp.trade.service;

import java.util.*;
import java.util.concurrent.TimeUnit;

import cn.dengta.webapp.base.service.BaseServiceImpl;
import cn.dengta.webapp.trade.dao.UserRecommendConfigDao;
import cn.dengta.webapp.trade.entity.UserRecommendConfig;
import me.codeplayer.util.*;
import org.springframework.stereotype.Service;

/**
 * 用户推荐分值配置表 服务实现类
 *
 * @author zw
 * @since 2022-10-20
 */
@Service
public class UserRecommendConfigService extends BaseServiceImpl<UserRecommendConfig, UserRecommendConfigDao, Long> {

	final LazyCacheLoader<Map<Integer, UserRecommendConfig>> cacheLoader = new TimeBasedCacheLoader<>(TimeUnit.DAYS.toDays(1L), this::find);

	public Map<Integer, UserRecommendConfig> find() {
		List<UserRecommendConfig> list = findAll();
		Map<Integer, UserRecommendConfig> map = new HashMap<>(list.size(), 1F);
		if (X.isValid(list)) {
			for (UserRecommendConfig config : list) {
				if (config.getType() != null) {
					map.put(config.getType(), config);
				}
			}
		}
		return map;
	}

	public UserRecommendConfig getByType(Integer type) {
		return cacheLoader.get().get(type);
	}

}
